package com.example.smartlearning.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.example.smartlearning.dao.CollectionDao;
import com.example.smartlearning.entity.LessonCollectionEntity;
import com.example.smartlearning.entity.ResultEntity;
import com.example.smartlearning.service.CollectionService;
import jakarta.annotation.Resource;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;

@Service
public class CollectionServiceImpl extends ServiceImpl<CollectionDao, LessonCollectionEntity> implements CollectionService {
    private static final Logger logger = LoggerFactory.getLogger(CollectionServiceImpl.class);//日志。getLogger需要传入当前的类名

    @Resource
    CollectionDao collectionDao;

    /**
     * 获取用户的所有收藏课程
     *
     * @return 返回
     */
    @Override
    public String getCollection(int userId) {
        try {
            return ResultEntity.success(collectionDao.selectCollectionByUserId(userId));
        } catch (Exception e) {
            e.printStackTrace();
            logger.error(e.getLocalizedMessage());
            return ResultEntity.fail(-10004);
        }
    }

    /**
     * 新增用户收藏接口
     *
     * @param userId   用户id
     * @param lessonId 课程id
     * @return 返回
     */
    @Override
    public String addCollection(int userId, int lessonId) {
        try {
            return ResultEntity.success(collectionDao.addCollection(userId, lessonId));
        } catch (Exception e) {
            e.printStackTrace();
            logger.error(e.getLocalizedMessage());
            return ResultEntity.fail(-10004);
        }
    }

    /**
     * 取消收藏接口
     *
     * @param collectionId 收藏id
     * @return 返回
     */
    @Override
    public String cancelCollection(int collectionId) {
        try {
            return ResultEntity.success(collectionDao.cancelCollection(collectionId));
        } catch (Exception e) {
            e.printStackTrace();
            logger.error(e.getLocalizedMessage());
            return ResultEntity.fail(-10004);
        }
    }
}
